<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>genlsdata.m</title>
<link rel="stylesheet" type="text/css" href="../../m-syntax.css">
</head>
<body>
<code>
<span class=defun_kw>function</span>&nbsp;<span class=defun_out>[data,model]&nbsp;</span>=&nbsp;<span class=defun_name>genlsdata</span>(<span class=defun_in>&nbsp;dim,&nbsp;num_data,&nbsp;margin&nbsp;</span>)<br>
<span class=h1>%&nbsp;GENLSDATA&nbsp;Generates&nbsp;linearly&nbsp;separable&nbsp;binary&nbsp;data.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Synopsis:</span></span><br>
<span class=help>%&nbsp;&nbsp;data&nbsp;=&nbsp;genlsdata(dim,num_data,margin)</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Description:</span></span><br>
<span class=help>%&nbsp;&nbsp;It&nbsp;generates&nbsp;randomly&nbsp;binary&nbsp;labeled&nbsp;vectors&nbsp;which&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;are&nbsp;linearly&nbsp;separable&nbsp;with&nbsp;prescribed&nbsp;margin.&nbsp;</span><br>
<span class=help>%&nbsp;</span><br>
<span class=help>%&nbsp;<span class=help_field>Input:</span></span><br>
<span class=help>%&nbsp;&nbsp;dim&nbsp;[1x1]&nbsp;Data&nbsp;dimension.</span><br>
<span class=help>%&nbsp;&nbsp;num_data&nbsp;[1x1]&nbsp;Number&nbsp;of&nbsp;generated&nbsp;data.</span><br>
<span class=help>%&nbsp;&nbsp;margin&nbsp;[1x1]&nbsp;Minimal&nbsp;ensured&nbsp;margin&nbsp;(distance&nbsp;of&nbsp;the&nbsp;closest</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;vector&nbsp;to&nbsp;the&nbsp;separating&nbsp;hyperplane).</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Output:</span></span><br>
<span class=help>%&nbsp;&nbsp;data&nbsp;[struct]&nbsp;Generated&nbsp;data:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.X&nbsp;[dim&nbsp;x&nbsp;num_data]&nbsp;Sample&nbsp;data.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.y&nbsp;[1&nbsp;x&nbsp;num_data]&nbsp;Data&nbsp;labels&nbsp;(1&nbsp;or&nbsp;2).</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;[struct]&nbsp;Ground&nbsp;truth&nbsp;linear&nbsp;classifier:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.W&nbsp;[dim&nbsp;x&nbsp;1]&nbsp;Normal&nbsp;vector&nbsp;of&nbsp;separating&nbsp;hyperplane.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.b&nbsp;[1x1]&nbsp;Bias&nbsp;of&nbsp;the&nbsp;hyperplane.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Example:</span></span><br>
<span class=help>%&nbsp;&nbsp;data&nbsp;=&nbsp;genlsdata(2,50,1);</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;ekozinec(&nbsp;data&nbsp;);</span><br>
<span class=help>%&nbsp;&nbsp;model.margin</span><br>
<span class=help>%&nbsp;&nbsp;figure;&nbsp;ppatterns(data);&nbsp;pline(model);</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;See&nbsp;also&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;PERCEPTRON,&nbsp;EKOZINEC,&nbsp;LINCLASS,&nbsp;SVM.</span><br>
<span class=help>%</span><br>
<hr>
<span class=help1>%&nbsp;<span class=help1_field>About:</span>&nbsp;Statistical&nbsp;Pattern&nbsp;Recognition&nbsp;Toolbox</span><br>
<span class=help1>%&nbsp;(C)&nbsp;1999-2003,&nbsp;Written&nbsp;by&nbsp;Vojtech&nbsp;Franc&nbsp;and&nbsp;Vaclav&nbsp;Hlavac</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.cvut.cz"&gt;Czech&nbsp;Technical&nbsp;University&nbsp;Prague&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.feld.cvut.cz"&gt;Faculty&nbsp;of&nbsp;Electrical&nbsp;Engineering&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://cmp.felk.cvut.cz"&gt;Center&nbsp;for&nbsp;Machine&nbsp;Perception&lt;/a&gt;</span><br>
<br>
<span class=help1>%&nbsp;<span class=help1_field>Modifications:</span></span><br>
<span class=help1>%&nbsp;3-may-2004,&nbsp;VF</span><br>
<span class=help1>%&nbsp;16-Feb-2003,&nbsp;VF</span><br>
<span class=help1>%&nbsp;26-feb-2001&nbsp;V.Franc</span><br>
<br>
<hr>
data.X&nbsp;=&nbsp;2*rand(dim,&nbsp;num_data&nbsp;)-1;<br>
<span class=keyword>if</span>&nbsp;<span class=stack>nargin</span>&nbsp;==&nbsp;3,<br>
&nbsp;&nbsp;data.X&nbsp;=&nbsp;data.X*margin*10;<br>
<span class=keyword>end</span><br>
<br>
model.W&nbsp;=&nbsp;2*rand(dim,1)-1;<br>
model.b&nbsp;=&nbsp;0;<br>
data.y&nbsp;=&nbsp;linclass(data.X,model);<br>
model.b&nbsp;=&nbsp;rand(1);<br>
data.X&nbsp;=&nbsp;data.X&nbsp;-&nbsp;model.b/sum(model.W);<br>
<br>
<span class=keyword>if</span>&nbsp;<span class=stack>nargin</span>&nbsp;==3,<br>
<br>
&nbsp;&nbsp;inx1&nbsp;=&nbsp;find(data.y&nbsp;==&nbsp;1);<br>
&nbsp;&nbsp;proj1&nbsp;=&nbsp;(model.W'*data.X(:,inx1)&nbsp;+&nbsp;model.b)/norm(model.W);<br>
&nbsp;&nbsp;shift1&nbsp;=&nbsp;max([ones(1,length(inx1))*margin&nbsp;;&nbsp;proj1])&nbsp;-&nbsp;proj1;<br>
&nbsp;&nbsp;data.X(:,inx1)&nbsp;=&nbsp;data.X(:,inx1)&nbsp;+&nbsp;...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(model.W*ones(1,length(inx1))/norm(model.W)).*(ones(dim,1)*shift1);<br>
<br>
&nbsp;&nbsp;inx2&nbsp;=&nbsp;find(data.y&nbsp;==&nbsp;2);<br>
&nbsp;&nbsp;proj2&nbsp;=&nbsp;-(model.W'*data.X(:,inx2)&nbsp;+&nbsp;model.b)/norm(model.W);<br>
&nbsp;&nbsp;shift2&nbsp;=&nbsp;max([ones(1,length(inx2))*margin&nbsp;;&nbsp;proj2])&nbsp;-&nbsp;proj2;<br>
&nbsp;&nbsp;data.X(:,inx2)&nbsp;=&nbsp;data.X(:,inx2)&nbsp;-&nbsp;...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(model.W*ones(1,length(inx2))/norm(model.W)).*(ones(dim,1)*shift2);<br>
<span class=keyword>end</span><br>
<br>
<span class=jump>return</span>;<br>
<br>
</code>
